const TABLE_STATE = require('../../consts/consts').TABLE_STATE;
const PLAYER_STATE = require('../../consts/consts').PLAYER_STATE;
let code = require('../../consts/consts');
let player = require('../../models/player');
let mjLogic = require('./mjLogic')();
const YCMJ_OP_TYPE = require('../../consts/consts').YCMJ_OP_TYPE;
let Table = function(app,opts){
    this.app = app;
    this.channelService = app.get('channelService');
    this.roomMgr = this.app.get('roomMgr');
    this.redis_db = this.app.get('redis_db');
    this.channel = null;
    this.tid = opts.tid;
    this.type = opts.type || -1;
    this.maxPerson = opts.maxPerson || 0;
    this.maxSeatNum = opts.maxSeatNum || 0;
    this.opTime = 10;
    this.state = TABLE_STATE.GAME_WAIT;
    this.players = new Map();
    this.seatPlayer = [];
    this.curOperator = -1; 
    this.lastOperator = -1;
    this.creator = opts.uid || -1;
    this.banker = -1;
    this.baseScore = opts.baseScore || 1;
    this.isCanBP = opts.isCanBP || 1;
    this.gameNum = opts.gameNum || 8;
    this.curgameNum = 0;
    this.operator = [];
    this._cards = [];
    this._curOutCard = -1;
    this.init();
};
Table.prototype.init = function(){
    this.channel = this.channelService.getChannel(this.tid,true);
    
    this.seatPlayer = new Array(this.maxSeatNum);
    
    //this.banker = this.creator;
};
Table.prototype.gameReady = function(uid){
    console.log('===================gameReady==================');
    if(!uid){
        return {
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.PARAM
        }
    };
    let p = this.getPlayerByUid(uid);
    if(!p){
        return {
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.USER_NOT_SEAT
        };
    };
    if(p.state == PLAYER_STATE.PLAYER_READY){
        return {
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.USER_ISREADY
        };
    };
    p.state = PLAYER_STATE.PLAYER_READY;
    let opts = {
        code:code.OK_CODE.USER_READY_SUCCESS,
        gameId:code.GAME_ID.YCMJ,
        msg:{
            seatId :p.seatId,
            state:p.state
        }
    };
    this.channel.pushMessage('playerReady',opts);
    return {
        code:code.OK,
        gameId:code.GAME_ID.YCMJ,
        msg:code.OK_CODE.USER_READY_SUCCESS
    };
}
Table.prototype.joinTable = function(opts){
    console.log('===================joinTable==================');
    let uid = opts.uid;
    if(!uid){
        return {
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.PARAM
        }
    };
    let p = this.getPlayerByUid(uid);
    if(p){
        if(!this.channel.getMember(p.uid)){
            this.channel.add(p.uid, p.sid);
        };
        let roomInfo = this.getRoomInfo(p.uid);
        return {
            code:code.OK_CODE.JOIN_TABLE_SUCCESS,
            gameId:code.GAME_ID.YCMJ,
            msg:roomInfo
        }
    }else
    {
        p = new player(opts);
        if(!this.channel.getMember(p.uid)){
            this.channel.add(p.uid, p.sid);
        };
        this.gameSeat(p);
        let roomInfo = this.getRoomInfo(p.uid);
        return {
            code:code.OK_CODE.JOIN_TABLE_SUCCESS,
            gameId:code.GAME_ID.YCMJ,
            msg:roomInfo
        };
    };
};
Table.prototype.gameSeat = function(p){
    console.log('===================gameSeat==================');
    let seatId = -1;
    for(let i=0;i<this.seatPlayer.length;i++){
        let sp = this.seatPlayer[i];
        if(!sp){
            seatId = i;
            break;
        };
    };
    if(seatId != -1){
        p.seatId = seatId;
        this.seatPlayer[p.seatId] = p;
        let opts = {
            code:code.OK_CODE.JOIN_TABLE_SUCCESS,
            gameId:code.GAME_ID.YCMJ,
            msg:{
                img : p.img,
                uid:p.uid,
                seatId :p.seatId,
                name : p.name,
                sex:p.sex,
                state:p.state,
                score:p.score
            }
        };
        this.channel.pushMessage('playerJoin',opts);
    };
}
Table.prototype.getRoomInfo = function(uid){
    let roomConf = {
        maxSeatNum:this.maxSeatNum,
        baseScore:this.baseScore,
        isCanBP:this.isCanBP,
        gameNum:this.gameNum
    };
    let res = {
        tid:this.tid,
        creator:this.creator,
        roomConf:roomConf,
        banker:this.banker,
        curOperator:this.curOperator,
        curgameNum:this.curgameNum,
        seatPlayer:[]
    };
    for(let i = 0;i<this.seatPlayer.length;i++){
        let sp = this.seatPlayer[i];
        if(sp){
            res.seatPlayer.push({
                handCard:sp.handCard,
                seatId:sp.seatId,
                uid:sp.uid,
                img:sp.img,
                sex:sp.sex,
                name:sp.name,
                score:sp.score,
                outCard:sp.outCard,
                pgCard:sp.outCard,
                moCard:sp.moCard,
                state:sp.state
            }) 
        }
    }
    return res;
}
Table.prototype.getPlayerByUid = function(uid){

    for(let i=0;i<this.seatPlayer.length;i++){
        let sp = this.seatPlayer[i];
        if(sp && sp.uid == uid){
            return sp;
        }
    }
    return null;
};
Table.prototype.getPlayerLength = function(uid){

    let count = 0;
    for(let i=0;i<this.seatPlayer.length;i++){
        let sp = this.seatPlayer[i];
        if(sp ){
            count ++;
        }
    }
    return count;
};

Table.prototype.gameReset = function(){
    console.log('===================gameReset==================');
    this.operator = [];
    this._cards = [];
    this._curOutCard = -1;
    this.state = TABLE_STATE.GAME_WAIT;
    for(let i=0;i<this.seatPlayer.length;i++){
        let p = this.seatPlayer[i];
        if(p){
            p.state = PLAYER_STATE.PLAYER_SEAT;
            p.reset();
        };
    };
};

Table.prototype.gameExit = async function(opts){
    console.log('===================gameExit==================');
    let uid = opts.uid;
    let reason = opts.reason;
    if(!uid){
        return {
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.PARAM
        }
    };
    let p = this.getPlayerByUid(uid);
    if(!p){
        return {
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.USER_NOT_SEAT
        };
    };
    this.seatPlayer[p.seatId] = null;
    this.channel.leave(p.uid,p.sid);
    await this.redis_db.HMSETFieldAsync(p.uid,'serverType','hall');
    if(this.getPlayerLength() === 0){
        let key = 'ycmj:'+this.tid;
        try {
            await this.redis_db.delKeyAync(key);
            this.roomMgr.delTable(this.tid);
        } catch (error) {
            return {
                code:code.OK_CODE.GAME_EXIT,
                gameId:code.GAME_ID.YCMJ,
                msg:error.stack
            };
        };
    }else
    {
        this.channel.pushMessage('gameExit',{
            code:code.OK_CODE.GAME_EXIT,
            gameId:code.GAME_ID.YCMJ,
            msg:{
                seatId:p.seatId,
                reason:reason
            }
        });
    }
    return {
        code:code.OK_CODE.GAME_EXIT,
        gameId:code.GAME_ID.YCMJ,
        msg:''
    };
};
Table.prototype.outCard = function(opts){
    console.log('===================outCard==================');
    if(this.state != TABLE_STATE.GAME_IN){
        return { 
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.GAME_NOT_CURSTATE
        }
    }
    let p = this.getSeatPlayerByUid(opts.uid);
    if(!p){
        return {
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.USER_NOT_EXIST
        }
    };
    if(p.seatId != this.curOperator){
        return {
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.NOT_CUROPREATOR
        }
    };
    let handCard = p.handCard;
    console.log('p.moCard----->',p.moCard);
    if(p.moCard != -1){
        handCard.push(p.moCard);
        p.moCard = -1;
    };
    console.log('car------->',opts.card,'handCard--->',handCard)
    if(handCard.length == 0){
        return {
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.CARD_NOT_YOURHANDCARD
        }
    };
    let index = -1;
    for(let i=0;i<handCard.length;i++){
        if(handCard[i] == opts.card){
            index = i;
            break;
        }
    }
    if(index == -1 ) {
        return {
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.CARD_NOT_YOURHANDCARD
        }
    };
    handCard.splice(index,1);
    console.log('handCarhandCardd---slice>',handCard)
    this._curOutCard = opts.card;
    p.outCard.push(this._curOutCard);
    let result = {
        code:code.OK_CODE.OUT_CARD,
        gameId:code.GAME_ID.YCMJ,
        msg:{
            seatId:p.seatId,
            card:this._curOutCard
        }
    }
    this.channel.pushMessage('outCard',result); 
    let isOp = false;
    for(let i = 0;i < this.seatPlayer.length;i++){
        let sp = this.seatPlayer[i];
        if(sp && sp.state == PLAYER_STATE.PLAYER_PLAYERING && sp.seatId != p.seatId){
            let oprate = [];
            let peng = mjLogic.peng(sp.handCard,this._curOutCard);
            if(peng.length > 0){
                oprate = oprate.concat(peng);
            };
            /***
             * 上家打出的牌 自己 才能有吃
             */
            if(this.isshangjia(p.seatId,sp.seatId)){
                let chi = mjLogic.chi(sp.handCard,this._curOutCard);
                if(chi.length > 0){
                    oprate = oprate.concat(chi);
                }
            }
            let mgang = mjLogic.mgang(sp.handCard,this._curOutCard);
            if(mgang.length > 0){
                oprate = oprate.concat(mgang);
            }
            let pgang = mjLogic.pgang(sp.pgCard,this._curOutCard);
            if(pgang.length > 0){
                oprate = oprate.concat(pgang);
            }
            let hu = mjLogic.hu(sp.handCard,[this._curOutCard]);
            if(hu){
                hu.hutype = hu.type;
                hu.type = YCMJ_OP_TYPE.HU;
                oprate.push(hu);
            }
            console.log('oprate--------->',oprate);
            if(oprate.length > 0){
                sp.oprate = oprate;
                isOp = true;
                this.state = TABLE_STATE.GAME_WAIT_OPRETION;
                this.opTime = 10;
                //this.curOperator = sp.seatId;
                this.operator.push(sp.seatId);
                let ops = {
                    code:code.OK_CODE.GAME_OPTRATION,
                    gameId:code.GAME_ID.YCMJ,
                    msg:{
                        oprate:oprate,
                        seatId:sp.seatId
                    }
                };
                // this.channelService.pushMessageByUids('gameDealCard',res,[{
                //     uid:p.uid,
                //     sid:p.sid
                // }]);
                this.channel.pushMessage('gameOperate',ops);
                // this.channelService.pushMessageByUids('gameOperate',{
               
                // },[{
                //     uid:sp.uid,
                //     sid:sp.sid
                // }]);
            }
        };
    };
    if(this._cards.length > 0){
        if(!isOp){
            this.setNextOutCardPlayer(p.seatId);
            if(this.curOperator != -1)
            {
                this.handleNextOutCard({
                    seatId:p.seatId
                })
            };
        };
    }else
    {
        //流局
        this.curOperator = -1;
        this.opTime = 0;
        this.state = TABLE_STATE.GAME_SMALL_SETTLEMENT;
    };
    return {
        code:code.OK_CODE.OUT_CARD,
        gameId:code.GAME_ID.YCMJ,
        msg:''
    }
};
Table.prototype.isshangjia = function(orgSeatId,tarSeatId){
    let sjSeatId = orgSeatId;
    sjSeatId --;
    if(sjSeatId < 0) sjSeatId = this.seatPlayer.length - 1;
    let p = this.seatPlayer[sjSeatId];
    while(!p){
        sjSeatId --;
        if(sjSeatId < 0) sjSeatId = this.seatPlayer.length - 1;
        p = this.seatPlayer[sjSeatId];
    }
    return sjSeatId == tarSeatId;
};
Table.prototype.handleNextOutCard = function(opts){
    this.opTime = 10;
    let p = this.seatPlayer[this.curOperator];
    if(this._cards.length > 0){
        let card = this._cards[0];
        p.moCard = card;
        this.lastOperator = opts.seatId;
        this._cards.splice(0,1);
        this.notiyOutCard(card);
        let agang = mjLogic.agang(p.handCard,card);
        let oprate = [];
        if(agang.length > 0){
            oprate = oprate.concat(mgang);
        };
        let hu = mjLogic.hu(p.handCard,[card]);
        if(hu){
            hu.hutype = hu.type;
            hu.type = YCMJ_OP_TYPE.ZIMO;
            oprate.push(hu);
        };
        if(oprate.length > 0){
            p.oprate = oprate;
            this.state = TABLE_STATE.GAME_WAIT_OPRETION;
            this.opTime = 10;
            this.operator.push(p.seatId);
            let ops = {
                code:code.OK_CODE.GAME_OPTRATION,
                gameId:code.GAME_ID.YCMJ,
                msg:{
                    oprate:oprate,
                    seatId:p.seatId
                }
            };
            // this.channelService.pushMessageByUids('gameDealCard',res,[{
            //     uid:p.uid,
            //     sid:p.sid
            // }]);
            this.channel.pushMessage('gameOperate',ops);
            // this.channelService.pushMessageByUids('gameOperate',{
           
            // },[{
            //     uid:sp.uid,
            //     sid:sp.sid
            // }]);
        }
    }else
    {
        this.curOperator = -1;
        /**
         * 游戏结束
         */
    }
    
};
Table.prototype.setNextOutCardPlayer = function(seatId){
    seatId --;
    if(seatId < 0) seatId = this.seatPlayer.length -1;
    let p = this.seatPlayer[seatId];
    if(p && p.state == PLAYER_STATE.PLAYER_PLAYERING)
    {
        this.curOperator = seatId;
        return;
    }else
    {
        this.setNextOutCardPlayer(seatId);
    }
}
Table.prototype.notiyOutCardStart = function(){
    if(this._cards.length > 0){
        let p = this.getPlayerByUid(this.creator);
        this.banker = p.seatId;
        this.curOperator = this.banker;
        this.lastOperator = -1 ;
        let moCard = this._cards[0];
        p.moCard = moCard;
        this._cards.splice(0,1);
        this.notiyOutCard(moCard);
    }
}
Table.prototype.notiyOutCard = function(moCard){
    console.log('===================notiyOutCard==================');
    /*
    *
    *通知类型 1 正常 
    * 
    * 
    * 
    */
   if(this._cards.length > 0){
    let opts = {
        code:code.OK_CODE.NOTIY_OUT_CARD,
        gameId:code.GAME_ID.YCMJ,
        msg:{
            curOperator:this.curOperator,
            lastOperator:this.lastOperator,
            moCard:moCard,
            state:this.state,
            opTime:this.opTime
        }
    };
     this.channel.pushMessage('notiyOutCard',opts);
   }
};
Table.prototype.notiyOperation = function(){
    console.log('===================notiyOutCard==================');
    /*
    *
    *通知类型 1 正常 
    * 
    * 
    * 
    */
   //this.curOperator = this.banker;
   let opts = {
       code:code.OK_CODE.NOTIY_OUT_CARD,
       gameId:code.GAME_ID.YCMJ,
       msg:{
        curOperator:this.curOperator
       }
   };
    this.channel.pushMessage('notiyOperation',opts);
};
Table.prototype.gameStart = function(){
    console.log('===================gameStart==================');
    this.curgameNum ++;
    for(let i=0;i<this.seatPlayer.length;i++){
        let sp = this.seatPlayer[i];
        if(sp && sp.state == PLAYER_STATE.PLAYER_READY){
            sp.state = PLAYER_STATE.PLAYER_PLAYERING;
        }
    };
    this.opTime = 5;
    let opts = {
        code : code.OK_CODE.GAME_START,
        gameId:code.GAME_ID.YCMJ,
        msg:{
            opTime:this.opTime
        }
    }
    this.channel.pushMessage('gameStart',opts);
};
Table.prototype.gameDealCard = function(){
    console.log('===================gameDealCard==================');
    this.opTime = 5;
    this._cards = mjLogic.shuffle();
    for(let i=0;i<this.seatPlayer.length;i++){
        let sp = this.seatPlayer[i];
        if(sp && sp.state == PLAYER_STATE.PLAYER_PLAYERING){
            sp.handCard = this._cards.slice(0,13);
            this._cards.splice(0,13);
        };
    };
    for(let i=0;i<this.seatPlayer.length;i++){
        let p = this.seatPlayer[i];
        if(p ){
            let res = {
                code:code.OK_CODE.GAME_DEAL_CARD,
                gameId:code.GAME_ID.YCMJ,
                msg:[]
            };
            for(let i=0;i<this.seatPlayer.length;i++){
                let sp = this.seatPlayer[i];
                if(sp.uid === p.uid){
                    res.msg.push({
                        seatId:sp.seatId,
                        handCard:sp.handCard
                    });
                }else{
                    res.msg.push({
                        seatId:sp.seatId,
                        handCard:sp.handCard.length
                    });
                };
            };
            this.channelService.pushMessageByUids('gameDealCard',res,[{
                uid:p.uid,
                sid:p.sid
            }]);
        };
    };
};
Table.prototype.gameSmallSett = function(){
    console.log('===================gameSmallSett==================');
    let msg = [];
    for(let i=0;i<this.seatPlayer.length;i++){
        let p = this.seatPlayer[i];
        if(p){
            msg.push({
                score:p.score,
                handCard:p.handCard,
                pgCard:pgCard,
                name:p.name,
                img:p.img,
                sex:p.sex
            })
        };
    }
    let ops = {
        code:code.OK_CODE.GAME_SMALL_SETTLEMENT,
        gameId:code.GAME_ID.YCMJ,
        msg:msg
    };
    this.channel.pushMessage('gameSmallSett',ops);
    this.opTime = 5;
    this.gameReset();
    if(this.curgameNum === this.gameNum){
        this.state = TABLE_STATE.GAME_BIG_SETTLEMENT;
    }
};
Table.prototype.gameBigSett = function(){
    console.log('===================gameBigSett==================');
    let ops = {
        code:code.OK_CODE.GAME_BIG_SETTLEMENT,
        gameId:code.GAME_ID.YCMJ,
        msg:[]
    };
    this.channel.pushMessage('gameBigSett',ops);
    
};
Table.prototype.gameDismiss = function(){
    console.log('===================gameDismiss==================');

};
Table.prototype.operate = function(opts){
    console.log('===================operate==================',this.state);
    if(this.state != TABLE_STATE.GAME_WAIT_OPRETION){
        return { 
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.GAME_NOT_CURSTATE
        }
    }
    let p = this.getSeatPlayerByUid(opts.uid);
    if(!p){
        return {
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:code.FAIL_CODE.USER_NOT_EXIST
        }
    };
    let type = opts.type;
    let optsp = {
        code:code.OK_CODE.GAME_OPTRATION,
        gameId:code.GAME_ID.YCMJ,
        msg:{
            seatId:p.seatId,
            type:type,
            score:[],
            card:''
        }
    };
    if(type == YCMJ_OP_TYPE.GUO){
        if(this._cards.length > 0){
            this.setNextOutCardPlayer(p.seatId);
            if(this.curOperator != -1)
            {
                this.handleNextOutCard({
                    seatId:p.seatId
                });
            };
        }else
        {
            //流局
            this.curOperator = -1;
            this.opTime = 0;
            this.state = TABLE_STATE.GAME_SMALL_SETTLEMENT;
        }
    }else if(type == YCMJ_OP_TYPE.HU || type == YCMJ_OP_TYPE.ZIMO){
        //流局
        this.curOperator = -1;
        this.opTime = 0;
        this.state = TABLE_STATE.GAME_SMALL_SETTLEMENT;
    }else 
    {
        let oprate = p.oprate;
        let handCard = p.handCard;
        console.log('===================cards==================',handCard);
        console.log('===================oprate==================',oprate);
        for(let j = 0;j<oprate.length;j++){
            let item = oprate[j];
            if(item.type == type){
                if(type == YCMJ_OP_TYPE.PENG){
                    let card = item.cards;
                    for(let i=0 ;i < 2 ;i++){
                        for(let i = handCard.length - 1;i >= 0;i --){
                            if(card === handCard[i]){
                                handCard.splice(i,1);
                                break;
                            };
                        }; 
                    }
                    p.pgCard.push(item);
                    optsp.msg.card = card;
                    this.curOperator = p.seatId;
                    this.notiyOutCard();
                    console.log('===================peng==================',handCard);
                }else if(type == YCMJ_OP_TYPE.ZUO_CHI || type == YCMJ_OP_TYPE.ZHONG_CHI || type == YCMJ_OP_TYPE.YOU_CHI){
                    let card = item.cards;
                    for(let j=0;j < card.length;j++){
                        for(let i = handCard.length - 1;i >= 0;i --){
                            if(card[j] == handCard[i]){
                                handCard.splice(i,1);
                                break; 
                            };
                        };
                    };
                    p.pgCard.push(item);
                    optsp.msg.card = card;
                    optsp.msg.lcard = item.card;
                    this.curOperator = p.seatId;
                    this.notiyOutCard();
                    console.log('===================ZUO_CHI==================',handCard);
                }else if(type == YCMJ_OP_TYPE.MING_GANG || type == YCMJ_OP_TYPE.AN_GANG){
                    let card = item.cards;
                    for(let i=0 ;i < 3 ;i++){
                        for(let i = handCard.length - 1 ;i >= 0;i --){
                            if(card === handCard[i]){
                                handCard.splice(i,1);
                                break;
                            };
                        }; 
                    }
                    p.pgCard.push(item);
                    for(let i=0;i<this.seatPlayer.length;i++){
                        let sp = this.seatPlayer[i];
                        if(sp && sp.seatId == p.seatId){
                            p.score += 1;
                            optsp.msg.score.push({
                                seatId:p.seatId,
                                score:p.score
                            })
                        }else if(sp){
                            p.score -= 1;
                            optsp.msg.score.push({
                                seatId:p.seatId,
                                score:p.score
                            })
                        };
                    }
                    optsp.msg.card = card;
                    this.curOperator = p.seatId;
                    if(this.curOperator != -1)
                    {
                        this.handleNextOutCard({
                            seatId:p.seatId
                        });
                    };
                    console.log('===================MING_GANG==================',handCard);
                }else if(type == YCMJ_OP_TYPE.PENG_GANG){
                    
                }
                break;
            }
        }
    };
    this.channel.pushMessage('gameOpration',optsp);
    p.operate = [];
    this.state = TABLE_STATE.GAME_IN;
    this.opTime = 10;
    return {
        code:code.OK_CODE.GAME_OPTRATION,
        gameId:code.GAME_ID.YCMJ,
        msg:{
            type:type
        }
    };
};
Table.prototype.getSeatPlayers = function(){
    let seatNum = 0;
    for(let i=0;i<this.seatPlayer.length;i++){
        let sp = this.seatPlayer[i];
        if(sp){
            seatNum ++;
        };
    };
    return seatNum;
};
Table.prototype.getSeatPlayerByUid = function(uid){
    for(let i=0;i<this.seatPlayer.length;i++){
        let sp = this.seatPlayer[i];
        if(sp && uid === sp.uid){
            return sp;
        };
    };
    return null;
};
Table.prototype.getReadPlayers = function(){
    let seatNum = 0;
    for(let i=0;i<this.seatPlayer.length;i++){
        let sp = this.seatPlayer[i];
        if(sp && sp.state == PLAYER_STATE.PLAYER_READY){
            seatNum ++;
        };
    };
    return seatNum;
};
Table.prototype.update = function(){
    
    if(this.state == TABLE_STATE.GAME_WAIT && this.getReadPlayers() == this.maxSeatNum){
        this.state = TABLE_STATE.GAME_READY;
        this.gameStart();
    }else if(this.state == TABLE_STATE.GAME_READY && this.opTime > 0){
        this.opTime --;
        if(this.opTime == 0 ){
            this.state = TABLE_STATE.GAME_START;
            this.gameDealCard();
        }
    }else if(this.state == TABLE_STATE.GAME_START && this.opTime > 0){
        this.opTime --;
        if(this.opTime == 0 ){
            this.state = TABLE_STATE.GAME_IN;
            this.notiyOutCardStart();
            this.opTime = 10;
        };
    }else if(this.state == TABLE_STATE.GAME_IN && this.opTime > 0 && this.curOperator != -1){
        // this.opTime --;
        // if(this.opTime == 0 ){
        //     let p = this.seatPlayer[this.curOperator];
        //     let handCard = p.handCard;
        //     if(handCard instanceof Array &&  handCard.length > 0){
        //         handCard = mjLogic.sort(handCard);
        //         let card = handCard[0];
        //         this.outCard({
        //             uid:p.uid,
        //             card:card
        //         });
        //     }else
        //     {
        //         this.curOperator = -1;
        //         this.opTime = 0;
        //         this.state = TABLE_STATE.GAME_SMALL_SETTLEMENT;
        //     }
        //     //this.notiyOutCard();
        //     this.opTime = 10;
        // };
    }else if(this.state == TABLE_STATE.GAME_WAIT_OPRETION && this.opTime > 0 && this.curOperator != -1){
        this.opTime --;
        if(this.opTime == 0 ){
            //this.notiyOutCard();
            for(let i=0;i<this.operator.length;i++){
                let curOpeate = this.operator[i];
                let p = this.seatPlayer[curOpeate];
                this.operate({
                    uid:p.uid,
                    type:YCMJ_OP_TYPE.GUO
                });
            };
            this.state = TABLE_STATE.GAME_IN;
            this.opTime = 10;
        };
    }else if(this.state == TABLE_STATE.GAME_SMALL_SETTLEMENT && this.opTime > 0){
        this.opTime --;
        if(this.opTime == 0 ){
            this.gameSmallSett();
        };
    }
};
module.exports = function(app,opts){
    return new Table(app,opts);
};
